﻿$(document).ready(function () {
    $('#mainSplitter').jqxSplitter({ width: '100%', height: '100%', theme: theme, panels: [{ size: '30%' }] });
    $("#jqxDockPanel").jqxDockPanel({ width: '100%', height: '100%', theme: theme });
    $('#listReport').jqxListMenu({
        width: '100%', height: '100%', enableScrolling: false,
        theme: theme, showHeader: true, showBackButton: true, showFilter: false
    });

    $("#BeginDate").jqxDateTimeInput({
        width: '250px',culture:'ru-RU',
        height: '25px', theme: theme,
        maxDate: new $.jqx._jqxDateTimeInput.getDateTime(new Date())

    });
    //инициализация дерева пользователя
    $.ajax({
        url: "/data/detailscar", dataType: "json", type: 'get',
        contentType: 'application/json; charset=UTF-8'
    }).done(function (data) {
        for (var key in data) {
            var curElem = data[key];
            curElem.Name = '<img class=\'' + curElem.Class + '\' /><span>' + curElem.Name + '</span>';
        }
        var source = {
            datatype: 'array',
            datafields: [
                { name: 'Id' },
                { name: 'ParentId' },
                { name: 'Name' }
            ],
            id: 'Id',
            localdata: data
        };
        // create data adapter.
        var carDataAdapter = new $.jqx.dataAdapter(source);
        // perform Data Binding.
        carDataAdapter.dataBind();
        var carRecords = carDataAdapter.getRecordsHierarchy('Id', 'ParentId', 'items', [{ name: 'Name', map: 'label' }, { name: 'Id', map: 'id' }]);
        $('#carTree').jqxTree({ source: carRecords, width: '100%',height: '100%', checkboxes: true, hasThreeStates: true, allowDrag: false, theme: theme });
    });
    



    $('.report_refer').each(function () {
        $("#filmPicture1").jqxTooltip({ content: '<b>Title:</b> <i>The Amazing Spider-man</i><br /><b>Year:</b> 2012', position: 'mouse', name: 'movieTooltip', theme: theme });
        $('#' + this.id).jqxTooltip({ content: this.title, position: 'mouse', name: 'movieTooltip', theme: theme });
        this.title = '';
        this.onclick = function () {
            var sendData = new Object;
            sendData.massiveIdInts = new Array();
            var elemetns = $('#carTree').jqxTree('getItems');
            for (var key in elemetns) {
                var item = elemetns[key];
                if (item.id.indexOf('tree_') == -1&&item.checked) {
                    sendData.massiveIdInts.push(item.id);
                }
            }

            sendData.beginDateTime = $('#BeginDate').jqxDateTimeInput('getDate');
            //data.ETime = $('#EndDate').data('datebox').theDate;            
            sendData.typeI = parseInt(this.name);
            $.ajax({
                url: "/Home/GetReports/",
                type: "POST",
                cache: false,
                data: JSON.stringify(sendData),
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',
                success: function (data) {
                    if (data) {
                        $.fileDownload('/Home/GetReports', { httpMethod: "GET" });
                    } else {
                        alert('Проверьте исходные данные');
                    }
                },
                error: function () {
                    alert('Произошел сбой при подключении');
                }
            });
        };
    });
});